package com.xuexi.mashibing;

import org.openjdk.jol.info.ClassLayout;

/**
 * 第三问：对象在内存中的布局
 * <p>
 * 对象头：object header（markword+classpointer）
 * markword  8字节
 * classpointer（类型指针）  4个字节
 * [如果是数据：多一个数组长度length  4个字节]
 * instancedata（实例数据，对象的属性）
 * padding（对齐）（64位系统8字节对齐，补齐能被8整除）
 * <p>
 * OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
 * 0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
 * 4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)  "markwork"
 * 8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)  "classpointer"
 * 12     4        (loss due to the next object alignment)
 * Instance size: 16 bytes
 * Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
 */
public class ObjectLayout {

    public static void main(String[] args) {

        /**
         * java.lang.Object object internals:
         *  OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
         *       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
         *       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
         *       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
         *      12     4        (loss due to the next object alignment)
         * Instance size: 16 bytes
         * Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
         */
        Object o = new Object();
        String printable = ClassLayout.parseInstance(o).toPrintable();
        System.out.println(printable);


        /**
         *  OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
         *       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
         *       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
         *       8     4        (object header)                           6d 01 00 f8 (01101101 00000001 00000000 11111000) (-134217363)
         *      12     4        (object header)                           07 00 00 00 (00000111 00000000 00000000 00000000) (7)
         *      16    28    int [I.<elements>                             N/A
         *      44     4        (loss due to the next object alignment)
         * Instance size: 48 bytes
         * Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
         */
        int[] a = new int[7];
        String printable1 = ClassLayout.parseInstance(a).toPrintable();
        System.out.println(printable1);

    }

    /*
     * markword 包含三部分信息
     * 1、锁信息
     *
     * 2、GC信息
     *
     * 3、HashCode
     * */
}
